package com.sf.osms.dep.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.sf.novatar.base.dao.BaseBeanDao;
import com.sf.novatar.base.dao.IPage;
import com.sf.novatar.base.domain.RequestParams;
import com.sf.osms.dep.dao.IAgentDao;
import com.sf.osms.dep.domain.Agent;

@Repository
public class IAgentDaoImpl extends BaseBeanDao<Long,Agent> implements IAgentDao{

	@Override
	public List<Agent> findAllAgents() {
		
		return this.findAll();
	}

	@Override
	public IPage<Agent> queryPage(RequestParams requestParams) {
		DetachedCriteria criteria = null;
		
		try {
			String countryCode = requestParams.getQueryValue("countryCode");//国家
			String name = requestParams.getQueryValue("name");//代理名称
			
			criteria = DetachedCriteria.forClass(Agent.class);
			if(StringUtils.isNotEmpty(countryCode)) {
				criteria.add(Restrictions.like("country",countryCode, MatchMode.ANYWHERE));
			}
			if(StringUtils.isNotEmpty(name)) {
				criteria.add(Restrictions.like("name",name, MatchMode.ANYWHERE));
			}
			
			criteria.addOrder(Order.desc("id"));
			
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}		
		return this.findPageBy(criteria, requestParams.getPagingParams());
	}

	@Override
	public Agent findAgentInfo(String name) {
		
		return null;
	}

}
